package org.dromara.setting.service.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lxk.service.core.Result;
import com.lxk.service.setting.api.OrderConfigApi;
import com.lxk.service.setting.response.OrderConfigResponse;
import com.lxk.service.setting.response.PayExpireTimeDto;
import lombok.AllArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.setting.domain.SettingConfigItem;
import org.dromara.setting.domain.bo.SettingConfigItemBo;
import org.dromara.setting.domain.vo.SettingConfigGroupVo;
import org.dromara.setting.domain.vo.SettingConfigItemVo;
import org.dromara.setting.enums.SettingAllEnums;
import org.dromara.setting.mapper.SettingConfigItemMapper;
import org.dromara.setting.service.ISettingConfigGroupService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class SettingConfigApiImpl implements OrderConfigApi {

    private final SettingConfigItemMapper settingConfigItemMapper;
    private final ISettingConfigGroupService settingConfigGroupService;

    @Override
    public Result<OrderConfigResponse> queryOrderConfig() {
        OrderConfigResponse resp = new OrderConfigResponse();
        SettingConfigItemBo itemBo = new SettingConfigItemBo();
        SettingConfigGroupVo queryByType = settingConfigGroupService.queryByType(SettingAllEnums.OmsOrderEnums.LXK_OMS_ORDER);
        if (ObjectUtil.isNull(queryByType)) {
            return Result.ok(resp);
        }
        itemBo.setConfigGroupId(queryByType.getId());
        LambdaQueryWrapper<SettingConfigItem> lqw = buildQueryWrapper(itemBo);
        List<SettingConfigItemVo> list = settingConfigItemMapper.selectVoList(lqw);
        if (CollUtil.isEmpty(list)) {
            return Result.ok(resp);
        }
        Map<String, String> map = list.stream().collect(Collectors.toMap(SettingConfigItemVo::getKey, v -> Optional.ofNullable(v.getValue()).orElse(BigDecimal.ZERO.toString())));
        resp.setAppId(Long.valueOf(map.get(SettingAllEnums.OmsOrderEnums.API_ID.getKey())));
        resp.setReceiveExpireTime(map.get(SettingAllEnums.OmsOrderEnums.RECEIVE_EXPIRE_TIME.getKey()));
        resp.setPayExpireTime(map.get(SettingAllEnums.OmsOrderEnums.PAY_EXPIRE_TIME.getKey()));
        resp.setCommentExpireTime(map.get(SettingAllEnums.OmsOrderEnums.COMMENT_EXPIRE_TIME.getKey()));
        return Result.ok(resp);
    }

    @Override
    public Result<List<PayExpireTimeDto>> getPayExpireTimeConfig() {
        List<SettingConfigItemVo> itemVoList = settingConfigItemMapper.selectVoList(new LambdaQueryWrapper<SettingConfigItem>()
            .eq(SettingConfigItem::getKey, SettingAllEnums.OmsOrderEnums.PAY_EXPIRE_TIME.getKey()));
        if (CollUtil.isEmpty(itemVoList)) {
            return Result.fail("配置不存在");
        }
        List<PayExpireTimeDto> list = itemVoList.stream().map(itemVo -> {
            PayExpireTimeDto dto = new PayExpireTimeDto();
            dto.setId(itemVo.getId());
            dto.setKey(itemVo.getKey());
            dto.setValue(itemVo.getValue());
            dto.setTenantId(itemVo.getTenantId());
            return dto;
        }).collect(Collectors.toList());
        return Result.ok(list);
    }


    private LambdaQueryWrapper<SettingConfigItem> buildQueryWrapper(SettingConfigItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SettingConfigItem> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(SettingConfigItem::getId);
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            SettingConfigItem::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            SettingConfigItem::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.like(StringUtils.isNotBlank(bo.getName()), SettingConfigItem::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getKey()), SettingConfigItem::getKey, bo.getKey());
        lqw.eq(StringUtils.isNotBlank(bo.getValue()), SettingConfigItem::getValue, bo.getValue());
        lqw.eq(bo.getSortNum() != null, SettingConfigItem::getSortNum, bo.getSortNum());
        lqw.eq(StringUtils.isNotBlank(bo.getValueInputType()), SettingConfigItem::getValueInputType, bo.getValueInputType());
        lqw.eq(bo.getConfigGroupId() != null, SettingConfigItem::getConfigGroupId, bo.getConfigGroupId());
        return lqw;
    }
}
